<template>
  <div class="search-container">
    <!-- 顶部搜索栏 -->
    <form class="search-from" action="/">
      <van-search
        v-model="searchText"
        background="#3296fa"
        show-action
        placeholder="请输入搜索关键词"
        @search="onSearch"
        @cancel="onCancel"
        @focus="isResultShow = false"
      />
    </form>
    <!-- 顶部搜索栏 -->

    <!-- 搜索结果 -->
    <SearchResult v-if="isResultShow"
    :search-text="searchText"
    ></SearchResult>
    <!-- 搜索结果 -->

    <!-- 联想建议 -->
    <!-- 输入框有内容，则渲染联想建议 -->
    <SearchSuggestion v-else-if="searchText"
    :search-text="searchText"
    @search="onSearch"
    ></SearchSuggestion>
    <!-- 联想建议 -->

    <!-- 搜索历史纪录 -->
    <SearchHistory v-else></SearchHistory>
    <!-- 搜索历史纪录 -->

  </div>
</template>

<script>
import SearchHistory from './components/search-history.vue'
import SearchResult from './components/search-result.vue'
import SearchSuggestion from './components/search-suggestion.vue'
export default {
  name: 'searchIndex',
  components: {
    SearchHistory,
    SearchResult,
    SearchSuggestion
  },
  data () {
    return {
      searchText: '',
      isResultShow: false // 控制搜索结果的展示
    }
  },

  mounted () {

  },

  methods: {
    onSearch (val) {
      this.searchText = val
      this.isResultShow = true
    },
    onCancel () {
      this.$router.back()
    }

  }
}
</script>

<style lang="less" scoped>

.search-container {
  padding-top: 54px;
  .van-search__action {
    color: #fff;
  }
  .search-from {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 1;
  }
}
</style>
